﻿@model IdentitySample.Models.LoginViewModel

@{
    ViewBag.Title = "Log in";
}

<div class="row">
    <!-- Page Header -->
    <div class="col-lg-12">
        <h1 class="page-header">@ViewBag.Title.</h1>
    </div>
    <!--End Page Header -->
</div>

<div class="row">
    <div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @id = "CreateProperty", @class = "form-horizontal", role = "form"}))
            {
                @Html.AntiForgeryToken()
                <h4>Use a local account to log in.</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @id = "loginEmail" })
                        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control", @id = "loginPassword" })
                        @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <button type="submit" class="btn btn-default">Log In</button>
                    </div>
                </div>
                <p>
                    @Html.ActionLink("Register a new user?", "Register")
                </p>
                <p>
                    @Html.ActionLink("Forget your password?", "ForgotPassword")
                </p>
            }
        </section>
    </div>
    <div class="col-md-4">
        <section id="socialLoginForm">
            @Html.Partial("_ExternalLoginsListPartial", new IdentitySample.Models.ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
        </section>
    </div>
</div>
@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @*@Scripts.Render("~/bundles/knockout")
    <script type="text/javascript" src="~/Scripts/Array.IndexOf.js"></script>
    <script type="text/javascript" src="~/Scripts/jquery_cookies.js"></script>
    <script type="text/javascript">

        // Validate Login - Login request before regular form is being submitted
        $("#UserAPICredentials").submit(function (e) {

        // this code prevents form from actually being submitted
        e.preventDefault(); //prevent default form submit
        e.returnValue = false;
        var $loginForm = $(this);

        var tokenKey = 'accessToken';
        //var token = $.cookie(tokenKey);
        //var headers = {};
        //if (token) {
        //    headers.Authorization = 'Bearer ' + token;
        //}
        var loginData = {
            grant_type: 'password',
            username: $("#loginEmail").val(),
            password: $("#loginPassword").val()
        };
        $.ajax({
            type: 'POST',
            url: '/Token',
            context: $loginForm, // context will be "this" in your handlers
            data: loginData
        }).done(function (data) {
            // Save Api Authentication Token to Cookie using "cookies.js" for entire site "path: '/'"
            $.cookie(tokenKey, data.access_token, { path: '/' });
            // make sure that you are no longer handling the submit event; clear handler
            this.off('submit');
            // actually submit the form
            this.submit();
        }).fail(function () {
            alert("error");
        })
    });
</script>*@
}